package com.idanchuang.component.consumer.ribbon.lb;

import org.springframework.cloud.client.loadbalancer.LoadBalancedRetryFactory;
import org.springframework.cloud.client.loadbalancer.LoadBalancedRetryPolicy;
import org.springframework.cloud.client.loadbalancer.ServiceInstanceChooser;
import org.springframework.cloud.netflix.ribbon.RibbonLoadBalancerContext;
import org.springframework.cloud.netflix.ribbon.SpringClientFactory;
import org.springframework.retry.RetryListener;
import org.springframework.retry.backoff.BackOffPolicy;

/**
 * @author yjy
 * Created at 2021/6/18 2:23 下午
 */
public class AcgRibbonLoadBalancedRetryFactory implements LoadBalancedRetryFactory {

    private final SpringClientFactory clientFactory;

    public AcgRibbonLoadBalancedRetryFactory(SpringClientFactory clientFactory) {
        this.clientFactory = clientFactory;
    }

    @Override
    public LoadBalancedRetryPolicy createRetryPolicy(String service,
                                                     ServiceInstanceChooser serviceInstanceChooser) {
        RibbonLoadBalancerContext lbContext = clientFactory
                .getLoadBalancerContext(service);
        return new AcgRibbonLoadBalancedRetryPolicy(service, lbContext,
                serviceInstanceChooser, clientFactory.getClientConfig(service));
    }

    @Override
    public RetryListener[] createRetryListeners(String service) {
        return new RetryListener[0];
    }

    @Override
    public BackOffPolicy createBackOffPolicy(String service) {
        return null;
    }

}
